//
//  ExpressionNode.m
//  MarketDataAnalyzer
//
//  Created by jiro on 10/01/23.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "ExpressionNode.h"


@implementation ExpressionNode

@synthesize type;

@synthesize lhs;
	
@synthesize rhs;

@synthesize value;


- (NSString*) toString
{
	switch ( type ) {
	case ExpressionNode_Value:
		return value;
	case ExpressionNode_Plus:
		return [ NSString stringWithFormat: @"(%@) + (%@)", [ lhs toString ], [ rhs toString ] ];
	case ExpressionNode_Minus:
		return [ NSString stringWithFormat: @"(%@) - (%@)", [ lhs toString ], [ rhs toString ] ];
	case ExpressionNode_Multiple:
		return [ NSString stringWithFormat: @"(%@) * (%@)", [ lhs toString ], [ rhs toString ] ];
	case ExpressionNode_Divide:
		return [ NSString stringWithFormat: @"(%@) / (%@)", [ lhs toString ], [ rhs toString ] ];
	case ExpressionNode_Modulous:
		return [ NSString stringWithFormat: @"(%@) % (%@)", [ lhs toString ], [ rhs toString ] ];
	default:
		NSAssert( NO, @"" );
		return @"";
	}
}

- (NSString*) toStringWithValuePrefix: (NSString*) prefix
{
	switch ( type ) {
	case ExpressionNode_Value:
		return [ NSString stringWithFormat: @"%@%@", prefix, value ];
	case ExpressionNode_Plus:
		return [ NSString stringWithFormat: @"(%@) + (%@)", [ lhs toStringWithValuePrefix: prefix ], [ rhs toStringWithValuePrefix: prefix ] ];
	case ExpressionNode_Minus:
		return [ NSString stringWithFormat: @"(%@) - (%@)", [ lhs toStringWithValuePrefix: prefix ], [ rhs toStringWithValuePrefix: prefix ] ];
	case ExpressionNode_Multiple:
		return [ NSString stringWithFormat: @"(%@) * (%@)", [ lhs toStringWithValuePrefix: prefix ], [ rhs toStringWithValuePrefix: prefix ] ];
	case ExpressionNode_Divide:
		return [ NSString stringWithFormat: @"(%@) / (%@)", [ lhs toStringWithValuePrefix: prefix ], [ rhs toStringWithValuePrefix: prefix ] ];
	case ExpressionNode_Modulous:
		return [ NSString stringWithFormat: @"(%@) % (%@)", [ lhs toStringWithValuePrefix: prefix ], [ rhs toStringWithValuePrefix: prefix ] ];
	default:
		NSAssert( NO, @"" );
		return @"";
	}
}

@end
